using UnityEditor;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.Rendering;

namespace CNGraphi.U6URP.Editor
{
    /// <summary>
    /// Lit 
    /// <para>作者：CN.Graphi</para>
    /// </summary>
    public class LitGUI : ShaderGraphGUI
    {
        bool bPBR = true;
        bool bAdvanced = true;

        protected override void DrawCustomSurfaceInput(Material material)
        {
            DrawFoldout(ref bPBR, "PBR", () =>
            {
                materialEditor.TexturePropertySingleLine(new GUIContent("Albedo"), FindProp("_AlbedoMap"), FindProp("_Color"));
                EditorGUI.indentLevel += 2;
                ShowProperty("_AlpClipThreshold", "Alpha Clip Threshold");
                EditorGUI.indentLevel -= 2;


                #region 法线
                EditorGUILayout.Space(5);
                MaterialProperty normalMapProp = FindProp("_NormalTex");
                materialEditor.TexturePropertySingleLine(new GUIContent("Normal"), normalMapProp, FindProp("_NormalStrength"));
                bool normalFlag = normalMapProp.textureValue != null;
                CoreUtils.SetKeyword(material, "_NORMALKEY", normalFlag);
                #endregion


                #region Mask
                EditorGUILayout.Space(5);
                MaterialProperty maskMapProp = FindProp("_MaskMap");
                materialEditor.TexturePropertySingleLine(new GUIContent("Mask"), maskMapProp);
                bool maskFlag = maskMapProp.textureValue != null;
                CoreUtils.SetKeyword(material, "_MASKKEY", maskFlag);
                EditorGUI.indentLevel += 2;
                if (maskFlag)
                {
                    materialEditor.MinMaxShaderProperty(FindProp("_MetalRemapping"), 0.0f, 1.0f, new GUIContent("Metallic Remapping"));
                    materialEditor.MinMaxShaderProperty(FindProp("_SmoothRemapping"), 0.0f, 1.0f, new GUIContent("Smoothing Remapping"));
                    materialEditor.MinMaxShaderProperty(FindProp("_AORemapping"), 0.0f, 1.0f, new GUIContent("AO Remapping"));
                }
                else
                {
                    ShowProperty("_Metallic");
                    ShowProperty("_Smoothness");
                    Tip("maskmap");
                }
                EditorGUI.indentLevel -= 2;
                #endregion


                #region 自发光
                EditorGUILayout.Space(10);
                ShowProperty("_Emission");
                MaterialProperty emissionFlag = FindProp("_Emission");
                bool emissionEnable = (emissionFlag.floatValue == 1.0f) ? true : false;
                CoreUtils.SetKeyword(material, "_EMISSIONKEY", emissionEnable);
                if (emissionEnable)
                {// 开启自发光
                    materialEditor.TexturePropertySingleLine(new GUIContent("EmissionMap"), FindProp("_EmissionMap"), FindProp("_EmissionColor"));
                    EditorGUI.indentLevel += 2;
                    ShowProperty("_MultiplyAlbedo");
                    EditorGUI.indentLevel -= 2;
                }
                #endregion


                #region 清漆
                EditorGUILayout.Space(10);
                MaterialProperty prop = FindProp("_CoatMap");
                bool canOperateClearcoat = prop.textureValue != null;
                CoreUtils.SetKeyword(material, "_CLEARCOATKEY", canOperateClearcoat);
                materialEditor.TexturePropertySingleLine(new GUIContent("Coat"), prop, canOperateClearcoat ? FindProp("_CoatFactor") : null);
                if (!canOperateClearcoat)
                {
                    Tip("coat");
                }
                #endregion


                EditorGUILayout.Space(5);
                ShowProperty("_TillingAndOffsets", "Tilling And Offsets");
            });


            EditorGUILayout.Space(12);
            DrawFoldout(ref bAdvanced, "Advanced", () =>
            {
                #region 细节
                MaterialProperty detailMapProp = FindProp("_DetailMap");
                materialEditor.TexturePropertySingleLine(new GUIContent("Detail"), detailMapProp);
                bool detailFlag = detailMapProp.textureValue != null;
                CoreUtils.SetKeyword(material, "_DETAILKEY", detailFlag);
                if (detailFlag)
                {
                    EditorGUI.indentLevel += 2;
                    EditorGUILayout.Space(3);
                    materialEditor.TexturePropertySingleLine(new GUIContent("Albedo"), FindProp("_DetailAlbedoMap"), FindProp("_DetailAlbedoMapColor"));
                    EditorGUILayout.Space(3);
                    ShowProperty("_DetailAlbedoScal", "Albedo Scale");
                    ShowProperty("_DetailNormalScal", "Normal Scale");
                    ShowProperty("_DetailSmoothnessScal", "Smoothing Scale");
                    EditorGUILayout.Space(3);
                    ShowProperty("_LockAlbedoTillingAndOffset", "Lock Albedo TillingAndOffset");
                    ShowProperty("_DetailTillingAndOffset", "Tilling And Offsets");
                    EditorGUI.indentLevel -= 2;
                }
                else
                {
                    Tip("detailed");
                }
                #endregion


                #region 菲涅尔
                EditorGUILayout.Space(10);
                MaterialProperty fresnelProp = FindProp("_Fresnel");
                ShowProperty(fresnelProp);
                bool fresnelFlag = fresnelProp.floatValue != 0;
                CoreUtils.SetKeyword(material, "_FRESNELKEY", fresnelFlag);
                if (fresnelFlag)
                {
                    EditorGUI.indentLevel++;
                    ShowProperty("_FresnelColor", "Color");
                    ShowProperty("_FresnelPower", "Power");
                    ShowProperty("_FresnelIntensity", "Intensity");
                    EditorGUI.indentLevel--;
                }
                #endregion
            });
        }


        private void Tip(string msg)
        {
            Gui.Tip($"The <i><b>{msg}</b></i> information is in the corresponding Shadergraph file");
        }
    }
}